package com.sunmouse.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;

import com.sunmouse.util.FileUtil;


public class HttpClient {
	static String des = "D://pic";
	static String src = "http://imgsrc.baidu.com/forum/w%3D580/sign=1974b8c8b21bb0518f24b320067bda77/8856faedab64034fc8f376ecaec379310b551d4f.jpg";

	public static void main(String[] args) {
		try {
			org.apache.http.client.HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(src);
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
				BufferedInputStream bis = new BufferedInputStream(response
						.getEntity().getContent());
				BufferedOutputStream bos = new BufferedOutputStream(
						new FileOutputStream(FileUtil.newImageFile(des)));
				byte[] b = new byte[1024];
				int len = 0;
				while ((len = bis.read(b)) != -1) {
					bos.write(b, 0, len);
					bos.flush();
				}
				bis.close();
				bos.close();
			}
		} catch (Exception e) {
			e.printStackTrace(); // TODO: handle exception
		}
	}
}
